﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Kokomo.IO
{
	[DebuggerDisplay("Position = {Position}")]
	public class LocalMemoryStream : Stream
	{
		public override bool CanRead
		{
			get { return true; }
		}

		public override bool CanSeek
		{
			get { return true; }
		}

		public override bool CanWrite
		{
			get { return true; }
		}

		public override void Flush()
		{
		}

		public override long Length
		{
			get { return Int32.MaxValue; }
		}

		public override long Position {get;set;}

		public override int Read(byte[] buffer, int offset, int count)
		{
			Marshal.Copy(new IntPtr(this.Position), buffer, offset, count);
			this.Position += count;
			return count;
		}

		public override long Seek(long offset, SeekOrigin origin)
		{
			long newPosition = this.Position;
			switch (origin)
			{
				case SeekOrigin.Begin:
					newPosition = offset;
					break;
				case SeekOrigin.Current:
					newPosition = this.Position + offset;
					break;
				case SeekOrigin.End:
					newPosition = this.Length + offset;
					break;
			}

			this.Position = newPosition;

			return this.Position;
		}

		public override void SetLength(long value)
		{
			throw new NotImplementedException();
		}

		public override void Write(byte[] buffer, int offset, int count)
		{
			Marshal.Copy(buffer, offset, new IntPtr(this.Position), count);
			this.Position += count;
		}
	}
}
